package com.alibaba.shopping_manager_api.controller;

import com.alibaba.shopping_common.pojo.Brand;
import com.alibaba.shopping_common.result.BaseResult;
import com.alibaba.shopping_common.service.BrandService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName BrandController
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/5 13:38
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    // dubbo注入
    @DubboReference
    private BrandService brandService;

    /**
     *
     * @param id 商品ID
     * @return 统一返回模型
     */
    @GetMapping("/findById")
    public BaseResult<Brand> findById(Long id){
        // 查询品牌信息
        Brand brand = brandService.findByID(id);
        return BaseResult.OK(brand);
    }

    /**
     * 查询所有的品牌信息
     * @return
     */
    @GetMapping("/all")
    public BaseResult<List<Brand>> all(){
        List<Brand> all = brandService.findAll();
        return BaseResult.OK(all);
    }

    /**
     * 新增品牌
     * @param brand
     * @return
     */
    @PostMapping("/add")
    public BaseResult<Brand> add(@RequestBody Brand brand){
        brandService.addBrand(brand);
        return BaseResult.OK(brand);
    }

    /**
     * 根据ID删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult<Long> delete(Long id){
        brandService.delBrand(id);
        return BaseResult.OK(id);
    }

    /**
     * 更新品牌信息
     * @param brand
     * @return
     */
    @PutMapping("/update")
    public BaseResult<Brand> update(@RequestBody Brand brand){
        brandService.updateBrand(brand);
        return BaseResult.OK(brand);
    }

    /**
     * 分页查询品牌
     * @param brand 查询条件
     * @param page 页码
     * @param size 每页条数
     * @return
     */
    @GetMapping("/search")
    public BaseResult<Page<Brand>> search(Brand brand,int page,int size){
        if (brand == null) {
            throw new IllegalArgumentException("品牌对象不能为空");
        }
        if (page < 0) {
            throw new IllegalArgumentException("页码不能为负数");
        }
        if (size <= 0 || size > 100) { // 假设最大分页大小为100
            throw new IllegalArgumentException("每页大小必须大于0且不超过100");
        }
        System.out.println("brand:"+brand);
        System.out.println("page:"+page);
        System.out.println("size:"+size);
        Page<Brand> search = brandService.search(brand, page, size);
        return BaseResult.OK(search);
    }
}




